package com.hsxc.cdz.monitor.handler;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.hsxc.cdz.base.util.Constants;
import com.hsxc.cdz.monitor.bean.MessageNew;
import com.hsxc.cdz.monitor.bean.pile.v1_21.BillPileMsg;
import com.hsxc.cdz.monitor.bean.pile.v1_21.CardPileMsg;
import com.hsxc.cdz.monitor.bean.pile.v1_21.CharageCeasePileMsg;
import com.hsxc.cdz.monitor.bean.pile.v1_21.ChargePileMsg;
import com.hsxc.cdz.monitor.bean.pile.v1_21.HeartPileMsg;
import com.hsxc.cdz.monitor.bean.pile.v1_21.PlusePileMsg;
import com.hsxc.cdz.monitor.bean.pile.v1_21.QRCSettingPileMsg;
import com.hsxc.cdz.monitor.bean.pile.v1_21.QueryRatePileMsg;
import com.hsxc.cdz.monitor.bean.pile.v1_21.RateSettingPileMsg;
import com.hsxc.cdz.monitor.bean.pile.v1_21.ReservPileMsg;
import com.hsxc.cdz.monitor.bean.pile.v1_21.ReserveCeasePileMsg;
import com.hsxc.cdz.monitor.bean.pile.v1_21.SignPileMsg;
import com.hsxc.cdz.monitor.bean.pile.v1_21.StateChangePileMsg;
import com.hsxc.cdz.monitor.bean.pile.v1_21.TelemetryPileMsg;
import com.hsxc.cdz.monitor.bean.pile.v1_33.UploadChargeDataPileMsg;
import com.hsxc.cdz.monitor.executor.PileExecutor;
import com.hsxc.cdz.monitor.executor.PileExecutorNew;

import io.netty.channel.ChannelHandler.Sharable;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.ReferenceCountUtil;

/**
 * @author 刘力文 处理充电桩请求业务逻辑的类
 */
@Sharable
@Component
public class PileInHandler extends ChannelInboundHandlerAdapter {

    private Logger logger = LoggerFactory.getLogger(PileInHandler.class);
    @Autowired
    PileExecutor pileExecutor;
    @Autowired
    PileExecutorNew pileExecutorNew;

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) {
        MessageNew message = (MessageNew) msg;

        boolean isSuccess = true;
        // 拿到业务报文帧
        String s = message.getFrame();

        if (message.getPileType() == 0 || message.getProtocolVersion().equals("0121")) {
            switch (s) {
                case Constants.CLIENT_SEND_HEART: // 心跳
                    logger.info("接受到心跳");
                    HeartPileMsg heartMsg = new HeartPileMsg();
                    heartMsg.getContentFromByte(message.getContent());
                    isSuccess = pileExecutor.heart(message.getAddress(), heartMsg, message.getLineNum(),
                            message.getProtocolVersion(), message.getPileType());
                    if (!isSuccess) {
                        ctx.close();
                    }
                    break;
                case Constants.CLIENT_SEND_START: // 充电桩主动上传充电请求
                    logger.info("开始充电电桩主动上送");
                    ChargePileMsg startContent = new ChargePileMsg();
                    startContent.getContentFromByte(message.getContent());
                    isSuccess = pileExecutor.startCharge(startContent, message.getAddress(), message.getLineNum(),
                            message.getProtocolVersion(), message.getPileType(), message.getResponse());
                    if (!isSuccess) {
                        ctx.close();
                    }
                    break;

                case Constants.CLIENT_SEND_STOP:// 充电桩主动上传停止充电请求
                    logger.info("充电桩主动上送停止充电");
                    CharageCeasePileMsg stop = new CharageCeasePileMsg();
                    stop.getContentFromByte(message.getContent());
                    isSuccess = pileExecutor.stopCharge(stop, message.getAddress(), message.getLineNum(),
                            message.getProtocolVersion(), message.getPileType());
                    if (!isSuccess) {
                        ctx.close();
                    }
                    break;
                case Constants.CLIENT_SEND_STOP_ORDER:// 充电桩主动停止预约
                    logger.info("充电桩主动停止预约");
                    ReserveCeasePileMsg stopOrder = new ReserveCeasePileMsg();
                    stopOrder.getContentFromByte(message.getContent());
                    pileExecutor.stopReserve(stopOrder, message.getAddress(), message.getLineNum(),
                            message.getProtocolVersion(), message.getPileType());
                    break;

                case Constants.CLIENT_SEND_UNLOAD:// 充电桩请求退出登录
                    logger.info("充电桩请求退出登录");
                    pileExecutor.unload(message.getAddress(), message.getOperator(), message.getLineNum(),
                            message.getProtocolVersion(), message.getPileType());
                    break;

                case Constants.CLIENT_SEND_CHANGE:// 充电桩上传遥变
                    logger.info("充电桩发送遥变信息");
                    StateChangePileMsg stateChangePM = new StateChangePileMsg();
                    stateChangePM.getContentFromByte(message.getContent());
                    pileExecutor.changeState(stateChangePM, message.getAddress());
                    break;

                case Constants.CLIENT_RESPONSE_ORDER:// 充电桩响应预约
                    logger.info("Pile is responsing reserv.");
                    ReservPileMsg reservPileMsg = new ReservPileMsg();
                    reservPileMsg.getContentFromByte(message.getContent());
                    pileExecutor.respReserve(reservPileMsg, message.getAddress(), message.getLineNum(),
                            message.getProtocolVersion(), message.getPileType());
                    break;

                case Constants.CLIENT_RESPONSE_RESTART:// 充电桩响应重启
                    pileExecutor.restart(message.getAddress());
                    break;
                case Constants.CLIENT_RESPONSE_QUERYRATE:// 充电桩响应查询费率
                    QueryRatePileMsg content = new QueryRatePileMsg();
                    content.getContentFromByte(message.getContent());
                    isSuccess = pileExecutor.queryFeeRate(message.getAddress(), content);
                    if (!isSuccess) {
                        ctx.close();
                    }
                    break;

                case Constants.CLIENT_RESPONSE_FITRATE:// 充电桩响应费率设置
                    RateSettingPileMsg rateSetting = new RateSettingPileMsg();
                    rateSetting.getContentFromByte(message.getContent());
                    logger.info("费率设置成功，无需处理");
                    break;

                case Constants.CLIENT_RESPONSE_QRC: // 设置二维码
                    logger.info("充电桩回复二维码设置");
                    QRCSettingPileMsg qrcSetting = new QRCSettingPileMsg();
                    qrcSetting.getContentFromByte(message.getContent());
                    logger.info("二维码是" + qrcSetting.getData().toString());
                    break;

                // 遥测数据
                case Constants.CLIENT_RESPONSE_STATE_TELEMETRY:
                    TelemetryPileMsg rmMsg = new TelemetryPileMsg();
                    rmMsg.getContentFromByte(message.getContent());
                    pileExecutor.getRemoteMetric(message.getAddress(), rmMsg);
                    break;
                // 遥信数据
                case Constants.CLIENT_RESPONSE_STATE_SIGN:
                    SignPileMsg rsMsg = new SignPileMsg();
                    rsMsg.getContentFromByte(message.getContent());
                    pileExecutor.getRemoteSignal(message.getAddress(), rsMsg);
                    break;
                // 遥脉数据
                case Constants.CLIENT_RESPONSE_STATE_PULSE:
                    PlusePileMsg rpMsg = new PlusePileMsg();
                    rpMsg.getContentFromByte(message.getContent());
                    pileExecutor.getRemotePulse(message.getAddress(), rpMsg);
                    break;

                case Constants.CLIENT_SEND_CARD:// 刷卡
                    logger.info("充电桩进行刷卡请求");
                    CardPileMsg card = new CardPileMsg();
                    card.getContentFromByte(message.getContent());
                    isSuccess = pileExecutor.card(card, message.getAddress(), message.getLineNum(),
                            message.getProtocolVersion(), message.getPileType());
                    if (!isSuccess) {
                        ctx.close();
                    }
                    break;
                case Constants.CLIENT_SEND_BILL:// 账单
                    logger.info("充电桩上传账单");
                    BillPileMsg billContent = new BillPileMsg();
                    billContent.getContentFromByte(message.getContent());
                    isSuccess = pileExecutor.storeBill(billContent, message.getAddress(), message.getLineNum());
                    if (!isSuccess) {
                        ctx.close();
                    }
                    break;
                case Constants.CLIENT_RESPONSE_CHECKTIME:
                    logger.info("充电桩回复对时操作，不需要处理");
                    break;
                default:
                    // ReferenceCountUtil.release(message);
            }
        } else {
            switch (s) {
                case Constants.CLIENT_SEND_HEART: // 心跳
                    // logger.info("接受到心跳");
                    // HeartPileMsg heartMsg = new HeartPileMsg();
                    // heartMsg.getContentFromByte(message.getContent());
                    // isSuccess = pileExecutor.heart(message.getAddress(),
                    // heartMsg, message.getLineNum(),
                    // message.getProtocolVersion(), message.getPileType());
                    // if (!isSuccess) {
                    // ctx.close();
                    // }
                    break;
                case Constants.CLIENT_SEND_START: // 充电桩主动上传充电请求
                    logger.info("开始充电电桩主动上送");
                    com.hsxc.cdz.monitor.bean.pile.v1_33.ChargePileMsg startContent = new com.hsxc.cdz.monitor.bean.pile.v1_33.ChargePileMsg();
                    startContent.getContentFromByte(message.getContent());
                    isSuccess = pileExecutorNew.startCharge(startContent, message.getAddress(), message.getLineNum(),
                            message.getProtocolVersion(), message.getPileType());
                    if (!isSuccess) {
                        ctx.close();
                    }
                    break;

                case Constants.CLIENT_SEND_STOP:// 充电桩主动上传停止充电请求
                    logger.info("充电桩主动上送停止充电");
                    com.hsxc.cdz.monitor.bean.pile.v1_33.CharageCeasePileMsg stop = new com.hsxc.cdz.monitor.bean.pile.v1_33.CharageCeasePileMsg();
                    stop.getContentFromByte(message.getContent());
                    isSuccess = pileExecutorNew.stopCharge(stop, message.getAddress(), message.getLineNum(),
                            message.getProtocolVersion(), message.getPileType());
                    if (!isSuccess) {
                        ctx.close();
                    }
                    break;

                case Constants.Upload_CHARGE_DATA_ZHILIU:
                    logger.info("直流充电桩发送充电数据");
                    com.hsxc.cdz.monitor.bean.pile.v1_33.UploadChargeDataPileMsg uploadChargeDataPileMsg = new UploadChargeDataPileMsg();
                    uploadChargeDataPileMsg.getContentFromByte(message.getContent());
                    pileExecutorNew.uploadChargeData(uploadChargeDataPileMsg, message.getAddress(), message.getLineNum(),
                            message.getProtocolVersion(), message.getPileType());
                    break;
                case Constants.CLIENT_SEND_STOP_ORDER:// 充电桩主动停止预约
                    logger.info("充电桩主动停止预约");
                    com.hsxc.cdz.monitor.bean.pile.v1_33.ReserveCeasePileMsg stopOrder = new com.hsxc.cdz.monitor.bean.pile.v1_33.ReserveCeasePileMsg();
                    stopOrder.getContentFromByte(message.getContent());
                    pileExecutorNew.stopReserve(stopOrder, message.getAddress(), message.getLineNum(),
                            message.getProtocolVersion(), message.getPileType());
                    break;

                case Constants.CLIENT_SEND_UNLOAD:// 充电桩请求退出登录
                    logger.info("充电桩请求退出登录");
                    pileExecutorNew.unload(message.getAddress(), message.getOperator(), message.getLineNum(),
                            message.getProtocolVersion(), message.getPileType());
                    break;

                case Constants.CLIENT_SEND_CHANGE:// 充电桩上传遥变
                    logger.info("充电桩发送遥变信息");
                    com.hsxc.cdz.monitor.bean.pile.v1_33.StateChangePileMsg stateChangePM = new com.hsxc.cdz.monitor.bean.pile.v1_33.StateChangePileMsg();
                    stateChangePM.getContentFromByte(message.getContent());
                    pileExecutorNew.changeState(stateChangePM, message.getAddress());
                    break;

                case Constants.CLIENT_RESPONSE_ORDER:// 充电桩响应预约
                    logger.info("Pile is responsing reserv.");
                    com.hsxc.cdz.monitor.bean.pile.v1_33.ReservPileMsg reservPileMsg = new com.hsxc.cdz.monitor.bean.pile.v1_33.ReservPileMsg();
                    reservPileMsg.getContentFromByte(message.getContent());
                    pileExecutorNew.respReserve(reservPileMsg, message.getAddress(), message.getLineNum(),
                            message.getProtocolVersion(), message.getPileType());
                    break;

                case Constants.CLIENT_RESPONSE_RESTART:// 充电桩响应重启
                    pileExecutorNew.restart(message.getAddress());
                    break;
                case Constants.CLIENT_RESPONSE_QUERYRATE:// 充电桩响应查询费率
                    com.hsxc.cdz.monitor.bean.pile.v1_33.QueryRatePileMsg content = new com.hsxc.cdz.monitor.bean.pile.v1_33.QueryRatePileMsg();
                    content.getContentFromByte(message.getContent());
                    isSuccess = pileExecutorNew.queryRate(message.getAddress(), content);
                    if (!isSuccess) {
                        ctx.close();
                    }
                    break;

                case Constants.CLIENT_RESPONSE_FITRATE:// 充电桩响应费率设置
                    // TODO
                    com.hsxc.cdz.monitor.bean.pile.v1_33.RateSettingPileMsg rateSetting = new com.hsxc.cdz.monitor.bean.pile.v1_33.RateSettingPileMsg();
                    rateSetting.getContentFromByte(message.getContent());

                    break;

                case Constants.CLIENT_RESPONSE_QRC: // 设置二维码
                    logger.info("充电桩回复二维码设置");
                    com.hsxc.cdz.monitor.bean.pile.v1_33.QRCSettingPileMsg qrcSetting = new com.hsxc.cdz.monitor.bean.pile.v1_33.QRCSettingPileMsg();
                    qrcSetting.getContentFromByte(message.getContent());
                    logger.info("电桩回复的二维码是" + qrcSetting.getData().toString());
                    break;

                // 遥测数据
                case Constants.CLIENT_RESPONSE_STATE_TELEMETRY:
                    // TelemetryPileMsg rmMsg = new TelemetryPileMsg();
                    // rmMsg.getContentFromByte(message.getContent());
                    // pileExecutor.getRemoteMetric(message.getAddress(), rmMsg);
                    break;
                // 遥信数据
                case Constants.CLIENT_RESPONSE_STATE_SIGN:
                    // SignPileMsg rsMsg = new SignPileMsg();
                    // rsMsg.getContentFromByte(message.getContent());
                    // pileExecutor.getRemoteSignal(message.getAddress(), rsMsg);
                    break;
                // 遥脉数据
                case Constants.CLIENT_RESPONSE_STATE_PULSE:
                    // PlusePileMsg rpMsg = new PlusePileMsg();
                    // rpMsg.getContentFromByte(message.getContent());
                    // pileExecutor.getRemotePulse(message.getAddress(), rpMsg);
                    break;

                case Constants.CLIENT_SEND_CARD:// 刷卡
                    logger.info("充电桩进行刷卡请求");
                    com.hsxc.cdz.monitor.bean.pile.v1_33.CardPileMsg card = new com.hsxc.cdz.monitor.bean.pile.v1_33.CardPileMsg();
                    card.getContentFromByte(message.getContent());
                    isSuccess = pileExecutorNew.card(card, message.getAddress(), message.getLineNum(),
                            message.getProtocolVersion(), message.getPileType());
                    if (!isSuccess) {
                        ctx.close();
                    }
                    break;
                case Constants.CLIENT_SEND_BILL:// 账单
                    logger.info("充电桩上传账单");
                    com.hsxc.cdz.monitor.bean.pile.v1_33.BillPileMsg billContent = new com.hsxc.cdz.monitor.bean.pile.v1_33.BillPileMsg();
                    billContent.getContentFromByte(message.getContent());
                    isSuccess = pileExecutorNew.storeBill(billContent, message.getAddress(), message.getLineNum());
                    if (!isSuccess) {
                        ctx.close();
                    }
                    break;
                case Constants.CLIENT_RESPONSE_CHECKTIME:
                    logger.info("充电桩回复对时操作，不需要处理");
                    break;
                default:
                    // ReferenceCountUtil.release(message);
            }
        }
        ReferenceCountUtil.release(msg);
    }
}
